﻿// Created by:  Tomer Shamam
// Date:        06-Feb-2009
// From:        http://blogs.microsoft.co.il/blogs/tomershamam/archive/2007/10/30/wpf-localization-on-the-fly-language-selection.aspx

using System;
using System.Linq;
using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Threading;
using System.ComponentModel;
using System.Xml;
using ScrumTable.Common;
using ScrumTable.Common.Xml;

namespace ScrumTable.UI.View.Localization
{
    public static class ScrumTableLanguage 
    { 

        #region Properties


        public static ObservableCollection<LanguageClass> SupportedLanguage
        {
            get
            {

               
                var languageList = new ObservableCollection<LanguageClass>();
                LanguageClass deCh = new LanguageClass
                                                 {
                                                     Language = "Deutsch",
                                                     CultureInfo = "de-CH",
                                                     ImageSource =
                                                         "pack://application:,,,/ScrumTable.UI.View;component/Resources/flag_german.png"
                                                 };
                languageList.Add(deCh);
                LanguageClass enUs = new LanguageClass
                                             {
                                                 Language = "English",
                                                 CultureInfo = "en-US",
                                                 ImageSource =
                                                     "pack://application:,,,/ScrumTable.UI.View;component/Resources/flag_english.png"
                                             };
                languageList.Add(enUs);
                var cultures = LanguageDictionary.RegisteredCultures.ToList();
                foreach (var culture in cultures)
                {
                    if (languageList.FirstOrDefault(x => x.CultureInfo == culture.Name) == null)
                    {

                        LanguageClass languageClass = new LanguageClass
                                                          {
                                                              Language = culture.DisplayName,
                                                              CultureInfo = culture.Name,
                                                              ImageSource = "pack://application:,,,/ScrumTable.UI.View;component/Resources/flag_white.png"
                                                          };
                        languageList.Add(languageClass);
                    }
                }

                return languageList;
            }
        }

       

        #endregion

        #region Method

            public static void RegisterDefaultDictionaries()
            {
                /*
                LanguageDictionary.RegisterDictionary(
                    CultureInfo.GetCultureInfo("en-US"),
                    new XmlLanguageDictionary("Localization/Languages/en-US.xml"));
                LanguageDictionary.RegisterDictionary(
                    CultureInfo.GetCultureInfo("de-CH"),
                    new XmlLanguageDictionary("Localization/Languages/de-CH.xml"));
                */

             
                foreach (var x in Directory.GetFiles("Localization/Languages/"))
                {
                    var xmlDoc = new XmlDocument();
                    xmlDoc.Load(x);
                    LanguageDictionary.RegisterDictionary(
                        CultureInfo.GetCultureInfo(xmlDoc.DocumentElement.GetAttributeValue("Culture")),
                        new XmlLanguageDictionary(x));
                }
                //<Dictionary EnglishName="German" CultureName="Deutsch" Culture="de-CH">
                //var culture = xmlDoc.DocumentElement.GetAttributeValue("Culture");
                //XmlReader.Create(Directory.GetFiles("Localization/Languages/")[0]);
                LanguageContext.Instance.Culture = CultureInfo.GetCultureInfo("en-US");

            }

        #endregion

        #region private classes

        public class LanguageClass
        {
            public string Language { get; set; }
            public string CultureInfo { get; set; }
            public string ImageSource { get; set; }
        }

        #endregion
    }
}